package io.zhengsh.order.bean;

import io.zhengsh.order.annotaion.Anno;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class BoyAndGirlMethodInterceptor implements MethodInterceptor {
    private Object bean;

    public Object warpBean(Object bean, String beanName) {
        Enhancer enhancer = new Enhancer();
        this.bean = bean;
        enhancer.setSuperclass(bean.getClass());
        enhancer.setCallback((Callback) this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object ret = null;
        if (method.getAnnotation(Anno.class) != null) {
            ret = methodProxy.invoke(bean, objects);
            System.out.println("Anno.......");
        } else {
            ret = methodProxy.invoke(bean, objects);
        }

        return ret;
    }

}